package com.example.business_oa.controller;



import cn.hutool.core.date.DateUtil;

import com.example.business_oa.service.BusiService;
import com.example.business_oa.service.FybxService;
import com.example.comment_oa.pojos.Budget;

import com.example.comment_oa.pojos.Standard;
import com.example.comment_oa.pojos.attendance.BusinessTripAp;
import com.example.comment_oa.pojos_vo.FybxVo;
import com.example.comment_oa.response.AjaxResponse;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/businessOa")
public class FybxController {
    @Resource
    FybxService fybxService;
    @Resource
    BusiService busiService;

    @RequestMapping("/Fybx")
    public AjaxResponse bxlcfq(@RequestBody FybxVo fybxVo){

        List<Standard> list1= fybxService.selectStand(fybxVo.getBudgetKm());
        //根据员工ID，跟发票开票天数获取出差天数
        List<BusinessTripAp> list=busiService.busiByIdEnd(fybxVo.getEmpId(), String.valueOf(fybxVo.getInvoiceTime()));
        Date date = new Date(String.valueOf(fybxVo.getBudgetTime()));
        String year = DateUtil.format(date, "yyyy");
        String month = DateUtil.format(date, "MM");
        List<Budget> budgetList=fybxService.selectByyscs(year,month,fybxVo.getDeptName(),fybxVo.getBudgetKm());
        if(list1.size()>0){
            System.out.println(year+""+month);
            if(fybxVo.getBudgetKm().equals("住宿费")){
               if(fybxVo.getInvoiceJshj()<=list1.get(0).getStandardBz()* list.get(0).getBtTime()&&fybxVo.getInvoiceJshj()<=budgetList.get(0).getBudgetCsmoney()){
                   //新增报销主表
                   fybxService.insertBXzb(fybxVo.getLcbh(),fybxVo.getAmountTitle(),new Date(),fybxVo.getDeptName()
                           ,fybxVo.getEmpId(),1,fybxVo.getInvoiceJshj(),fybxVo.getEmpCard(),fybxVo.getEmpName());

                   //修改发票状态
                   fybxService.updateinvoicePhone(fybxVo.getInvoicePhone());
                   //修改预算初始额
                   fybxService.updateCsys(fybxVo.getInvoiceJshj(),year,month,fybxVo.getDeptName(),fybxVo.getBudgetKm());
                   //新增报销详表
                   fybxService.insertBxxb(fybxVo.getBudgetKm(),fybxVo.getInvoiceJshj(),fybxVo.getInvoicePhone(),
                           fybxVo.getLcbh(),fybxVo.getDeptName(),fybxVo.getBudgetTime());

               }else{
                   return AjaxResponse.dosentexit();
               }

            }else{
                if(fybxVo.getInvoiceJshj()<list1.get(0).getStandardBz()&&fybxVo.getInvoiceJshj()<budgetList.get(0).getBudgetCsmoney()){
                    //新增报销主表
                    fybxService.insertBXzb(fybxVo.getLcbh(),fybxVo.getAmountTitle(),new Date(),fybxVo.getDeptName()
                            ,fybxVo.getEmpId(),1,fybxVo.getInvoiceJshj(),fybxVo.getEmpCard(),fybxVo.getEmpName());

                    //修改发票状态
                    fybxService.updateinvoicePhone(fybxVo.getInvoicePhone());
                    //修改预算初始额
                    fybxService.updateCsys(fybxVo.getInvoiceJshj(),year,month,fybxVo.getDeptName(),fybxVo.getBudgetKm());

                    //新增报销详表
                    fybxService.insertBxxb(fybxVo.getBudgetKm(),fybxVo.getInvoiceJshj(),fybxVo.getInvoicePhone(),
                            fybxVo.getLcbh(),fybxVo.getDeptName(),fybxVo.getBudgetTime());

                }else{
                    return AjaxResponse.dosentexit();
                }

            }
        }else{
            if(fybxVo.getInvoiceJshj()<budgetList.get(0).getBudgetCsmoney()){
                //新增报销主表
                fybxService.insertBXzb(fybxVo.getLcbh(),fybxVo.getAmountTitle(),new Date(),fybxVo.getDeptName()
                ,fybxVo.getEmpId(),1,fybxVo.getInvoiceJshj(),fybxVo.getEmpCard(),fybxVo.getEmpName());

                //修改发票状态
                fybxService.updateinvoicePhone(fybxVo.getInvoicePhone());
                //修改预算初始额
                fybxService.updateCsys(fybxVo.getInvoiceJshj(),year,month,fybxVo.getDeptName(),fybxVo.getBudgetKm());
                //新增报销详表
                fybxService.insertBxxb(fybxVo.getBudgetKm(),fybxVo.getInvoiceJshj(),fybxVo.getInvoicePhone(),
                        fybxVo.getLcbh(),fybxVo.getDeptName(),fybxVo.getBudgetTime());




            }else{
                    return AjaxResponse.dosentexit();
            }
        }
                return AjaxResponse.success();
    }


    @RequestMapping("/FybxSp")
    public AjaxResponse bxsp(@RequestBody FybxVo fybxVo){
        Date date = new Date(String.valueOf(fybxVo.getCdqj()));
        String year = DateUtil.format(date, "yyyy");
        String month = DateUtil.format(date, "MM");
        if(fybxVo.getPd()==1){
            fybxService.updatefazt(fybxVo.getInvoicePhone());
            //减少剩余额
            fybxService.updateSy(fybxVo.getAmountMoney(),year,month,fybxVo.getDeptName(),fybxVo.getBudgetKm());
            //修改报销流程状态
            fybxService.updatebxState(0,fybxVo.getAmountProcess());
        }else if(fybxVo.getPd()==2){
            fybxService.csyszj(fybxVo.getAmountMoney(),year,month,fybxVo.getDeptName(),fybxVo.getBudgetKm());
            //修改报销流程状态
            fybxService.updatebxState(2,fybxVo.getAmountProcess());
        }

        return AjaxResponse.success();

    }
}
